var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

class CrudService {
  constructor(db, ...arg) {
    this.db = db;
    this.arg = arg;
  }

  /**
     * 保存记录
     * @param tableName
     * @param values
     * @returns {*}
     */
  save(tableName, values) {
    var _this = this;

    return _asyncToGenerator(function* () {
      if (values instanceof Object && !Array.isArray(values)) {
        return _this.db[tableName].create(values);
      } else {
        return false;
      }
    })();
  }

  /**
     * 根据id更新记录
     * @param tableName
     * @param values
     * @param id
     * @returns {*}
     */
  updateById(tableName, values, id) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      if (values instanceof Object && !Array.isArray(values)) {
        return _this2.db[tableName].update(values, { where: { id: id } });
      }
    })();
  }

  /**
     * 自定义where条件更新记录
     * @param tableName
     * @param values
     * @param where
     * @returns {*}
     */
  updateByWhere(tableName, values, where) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      if (values instanceof Object && !Array.isArray(values)) {
        return _this3.db[tableName].update(values, { where: where });
      }
    })();
  }

  /**
     * 自定义where条件查询，返回原始对象数据，不带任何sequelize生命周期实例属性
     * @param tableName
     * @param where
     */
  findAll(tableName = '', where = {}, attributes = []) {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      (0, _assert2.default)(Array.isArray(attributes), 'attributes must be a Array');
      (0, _assert2.default)(typeof tableName === 'string', 'tableName must be a String');
      (0, _assert2.default)(ObjectUtils.isObject(where), 'where must be a Object');
      const option = {
        raw: true
      };
      if (Object.keys(where).length > 0) {
        option.where = where;
      }

      if (attributes.length > 0) {
        option.attributes = attributes;
      }
      return _this4.db[tableName].findAll(option);
    })();
  }

  /**
     * 原生sql查询
     * @param sql
     * @returns {*}
     */
  querySql(sql) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      return _this5.db.sequelize.query(sql, { type: _this5.db.sequelize.QueryTypes.SELECT });
    })();
  }

  /**
     *保存多条数据
     * @param tableName  模型
     * @param array 数组
     * @returns {*}
     */
  saveMany(tableName, array) {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      if (!Array.isArray(array)) {
        return false;
      }
      return _this6.db[tableName].bulkCreate(array);
    })();
  }

  /**
     * 根据id删除数据
     * @param tableName
     * @param id
     * @returns {*}
     */
  deleteById(tableName, id) {
    var _this7 = this;

    return _asyncToGenerator(function* () {
      return _this7.db[tableName].destroy({ where: { id: id } });
    })();
  }

  /**
     * 删除所有数据
     * @param tableName
     * @returns {*}
     */
  deleteAll(tableName) {
    var _this8 = this;

    return _asyncToGenerator(function* () {
      return _this8.db[tableName].destroy();
    })();
  }
}

module.exports = CrudService;